using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using MageDefender.Interfaces;

namespace MageDefender.GameObjects
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Fort : Microsoft.Xna.Framework.DrawableGameComponent, IFort
    {
        GameClasses.Castle theCastle;

        public GameClasses.Castle TheCastle
        {
            get { return theCastle; }
            set { theCastle = value; }
        }
        GameClasses.GameModel castleModel0;
        GameClasses.GameModel castleModel1;
        GameClasses.GameModel castleModel2;
        GameClasses.GameModel castleModel3;
        GameClasses.GameModel castleModel4;
        GameClasses.GameModel castleModel5;

        public GameClasses.GameModel CastleModel0
        {
            get { return castleModel0; }
            set { castleModel0 = value; }
        }

        public void NextWave()
        {
            //theCastle.Level++;
            theCastle.Wave++;

            if (theCastle.Wave == 5)
            {
                theCastle.IsCurrentWaveABoss = true;
            }
            else if (theCastle.Wave == 10)
            {
                theCastle.IsCurrentWaveABoss = true;
            }
            else theCastle.IsCurrentWaveABoss = false;

            if (theCastle.Wave > 10)
            {
                theCastle.Wave = 1;
            }



            if (!theCastle.IsCurrentWaveABoss)
            {
                theCastle.EnemiesInCurrentWave = 20 + (theCastle.Level * 5);
            }
            else theCastle.EnemiesInCurrentWave = 1;
        }

        public Fort(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
            castleModel0 = new GameClasses.GameModel(game);
            castleModel1 = new GameClasses.GameModel(game);
            castleModel2 = new GameClasses.GameModel(game);
            castleModel3 = new GameClasses.GameModel(game);
            castleModel4 = new GameClasses.GameModel(game);
            castleModel5 = new GameClasses.GameModel(game);

            theCastle = new GameClasses.Castle();
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public void Initialize(Matrix projMatrix, Matrix viewMatrix)
        {
            // TODO: Add your initialization code here
            castleModel0.Initialize("Models\\castle0", "Models\\castle_tex", "Textures\\Toon", projMatrix, viewMatrix);
            castleModel1.Initialize("Models\\castle1", "Models\\castle_tex", "Textures\\Toon", projMatrix, viewMatrix);
            castleModel2.Initialize("Models\\castle2", "Models\\castle_tex", "Textures\\Toon", projMatrix, viewMatrix);
            castleModel3.Initialize("Models\\castle3", "Models\\castle_tex", "Textures\\Toon", projMatrix, viewMatrix);
            castleModel4.Initialize("Models\\castle4", "Models\\castle_tex", "Textures\\Toon", projMatrix, viewMatrix);
            castleModel5.Initialize("Models\\castle5", "Models\\castle_tex", "Textures\\Toon", projMatrix, viewMatrix);

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Update(GameTime gameTime, Vector3 vecEye, Matrix viewMatrix)
        {
            // TODO: Add your update code here
            Matrix objectMatrix = Matrix.CreateRotationX(0) * Matrix.CreateRotationY(0) * Matrix.CreateTranslation(0.0f, 155.0f, 0.0f);
            Matrix renderMatrix = Matrix.CreateScale(0.0080f);
            renderMatrix = objectMatrix * renderMatrix;
            castleModel0.Update(vecEye, viewMatrix, renderMatrix, gameTime);
            castleModel1.Update(vecEye, viewMatrix, renderMatrix, gameTime);
            castleModel2.Update(vecEye, viewMatrix, renderMatrix, gameTime);
            castleModel3.Update(vecEye, viewMatrix, renderMatrix, gameTime);
            castleModel4.Update(vecEye, viewMatrix, renderMatrix, gameTime);
            castleModel5.Update(vecEye, viewMatrix, renderMatrix, gameTime);

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            switch(theCastle.Level)
            {
                case 1:
                    {
                        castleModel0.Draw(gameTime);
                        break;
                    }
                case 2:
                    {
                        castleModel1.Draw(gameTime);
                        break;
                    }
                case 3:
                    {
                        castleModel2.Draw(gameTime);
                        break;
                    }
                case 4:
                    {
                        castleModel3.Draw(gameTime);
                        break;
                    }
                case 5:
                    {
                        castleModel4.Draw(gameTime);
                        break;
                    }
                case 6:
                    {
                        castleModel5.Draw(gameTime);
                        break;
                    }
            }

            base.Draw(gameTime);
        }
    }
}